package com.example.controller;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class JobLauncherController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    // 	http://localhost:9999/resource/jobLauncher
    @GetMapping("/jobLauncher")
    public void handle() throws Exception{
        // 使用UUID作为参数的一部分，确保唯一性
        String uuid = UUID.randomUUID().toString();
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("uuid", uuid) // 添加UUID作为参数
                .toJobParameters();

        JobExecution execution = jobLauncher.run(job, jobParameters);
        System.out.println("Exit Status : " + execution.getExitStatus());
    }
}